package com.itheima.cs.common.dyewm;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import com.itheima.cs.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.itheima.cs.R;
import com.itheima.cs.adapter.dyewm.MaterialAdapter;
import com.itheima.cs.dao.MaterialDao;
import com.itheima.cs.entity.dyewm.MaterialInfo;
import com.itheima.cs.utils.LoginManager;

import java.util.ArrayList;
import java.util.List;

public class MaterialSelectActivity extends AppCompatActivity {

    private EditText etSearch;
    private MaterialAdapter adapter;
    private List<MaterialInfo> dataList = new ArrayList<>();
    private MaterialInfo selectedMaterial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_select);

        initToolbar();
        initViews();
    }

    private void initToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar_material_select);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initViews() {
        etSearch = findViewById(R.id.et_search);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new MaterialAdapter(dataList, this::onMaterialSelected);
        recyclerView.setAdapter(adapter);

        findViewById(R.id.btn_search).setOnClickListener(v -> performSearch());

        findViewById(R.id.btn_confirm).setOnClickListener(v -> {
            if (selectedMaterial != null) {
                Intent resultIntent = new Intent();
                resultIntent.putExtra("selected_material", (Parcelable) selectedMaterial);
                setResult(RESULT_OK, resultIntent);
                finish();
            }else {
                Toast.makeText(this, "未获取到物料", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @SuppressLint("StaticFieldLeak")
    private void performSearch() {
        String keyword = etSearch.getText().toString().trim();
        if (keyword.isEmpty()) {
            Toast.makeText(this, "请输入搜索关键词", Toast.LENGTH_SHORT).show();
            return;
        }

        new AsyncTask<String, Void, List<MaterialInfo>>() {
            @Override
            protected List<MaterialInfo> doInBackground(String... keywords) {
                String username = LoginManager.getInstance(MaterialSelectActivity.this).getUsername();
                return new MaterialDao().searchMaterials(keywords[0], username);
            }

            @Override
            protected void onPostExecute(List<MaterialInfo> result) {
                dataList.clear();
                dataList.addAll(result);
                adapter.setSelectedPosition(-1);
                selectedMaterial = null;
                adapter.notifyDataSetChanged();
            }
        }.execute(keyword);
    }

    private void onMaterialSelected(MaterialInfo material) {
        selectedMaterial = material;
    }
}
